package com.aiit.controller;
import com.aiit.mapper.UserMapper;
import com.aiit.pojo.User;
import com.aiit.service.UserService;
import com.aiit.util.Result;
import com.aiit.util.TableResult;
import com.aiit.vo.PageResultVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: LuTao
 * @Date: 2022/05/04/14:29
 */
@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;


    @ResponseBody
    @GetMapping("/user/getByCondition")
    public TableResult<User> getByCondition(@RequestParam("page")Long page,
                                            @RequestParam("limit")Long limit,
                                            @RequestParam("username")String username,
                                            @RequestParam("role")String role,
                                            @RequestParam("startTime")String startTime, @RequestParam(value = "endTime",required = false)String endTime,
                                            @RequestParam("locked")String locked){

        PageResultVo<User> pageResultVo = userService.getByCondition(page,limit,username,role,startTime,endTime,locked);
        return new TableResult(0,"",pageResultVo.getTotal(),pageResultVo.getRecords());
    }

    @PostMapping("/login")
    @ResponseBody
    public Result login(String username, String password, String code, HttpSession session){
         String codes= (String) session.getAttribute("KAPTCHA_SESSION_KEY");
//           if(StrUtil.hasBlank(codes)){
//            throw new RuntimeException("验证码超时!");
//           }
           String msg="";
        try {
            if (codes.equalsIgnoreCase(code)){
                //获取主体对象
                Subject subject = SecurityUtils.getSubject();
                subject.login(new UsernamePasswordToken(username, password));
                return Result.succ("登录成功");
            }else{
                throw new RuntimeException("验证码错误!");
            }
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            msg="用户名错误!";
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            msg="密码错误!";
        }catch (Exception e){
            e.printStackTrace();
            msg="连接超时！";
        }
          return Result.fail(msg);
    }

        @GetMapping("logout")
    public String logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "login";
    }

      @PostMapping("/register")
      @ResponseBody
      public Result register(User user){
          userService.register(user);
         return Result.succ("操作成功");
      }

}
